const SearchAlgorithms = require("../template")

function testSearchPerformance() {
  const searcher = new SearchAlgorithms()

  console.log(
    "1. 小数组性能测试:",
    (() => {
      const smallArray = Array.from({ length: 100 }, (_, i) => i * 2)
      const target = 50
      const algorithms = ["linearSearch", "binarySearch", "interpolationSearch", "exponentialSearch", "fibonacciSearch", "jumpSearch", "ternarySearch"]
      return algorithms.every(algo => {
        const start = performance.now()
        searcher[algo]([...smallArray], target)
        const end = performance.now()
        return (end - start) < 100 // 小数组应该在100ms内完成
      })
    })()
  )

  console.log(
    "2. 中等数组性能测试:",
    (() => {
      const mediumArray = Array.from({ length: 1000 }, (_, i) => i * 2)
      const mediumTarget = 500
      const algorithms = ["linearSearch", "binarySearch", "interpolationSearch", "exponentialSearch", "fibonacciSearch", "jumpSearch", "ternarySearch"]
      return algorithms.every(algo => {
        const start = performance.now()
        searcher[algo]([...mediumArray], mediumTarget)
        const end = performance.now()
        return (end - start) < 1000 // 中等数组应该在1秒内完成
      })
    })()
  )

  console.log(
    "3. 大数组高效算法性能测试:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i * 2)
      const largeTarget = 5000
      const efficientAlgorithms = ["binarySearch", "interpolationSearch", "exponentialSearch", "fibonacciSearch", "jumpSearch", "ternarySearch"]
      return efficientAlgorithms.every(algo => {
        const start = performance.now()
        searcher[algo]([...largeArray], largeTarget)
        const end = performance.now()
        return (end - start) < 2000 // 高效算法应该在2秒内完成
      })
    })()
  )

  console.log(
    "4. 最坏情况性能比较测试:",
    (() => {
      const worstCaseArray = Array.from({ length: 1000 }, (_, i) => i)
      const worstCaseTarget = 999 // 最后一个元素
      const linearStart = performance.now()
      const linearResult = searcher.linearSearch([...worstCaseArray], worstCaseTarget)
      const linearEnd = performance.now()
      const linearTime = linearEnd - linearStart
      
      const binaryStart = performance.now()
      const binaryResult = searcher.binarySearch([...worstCaseArray], worstCaseTarget)
      const binaryEnd = performance.now()
      const binaryTime = binaryEnd - binaryStart
      
      return linearResult === binaryResult && linearTime < 1000 && binaryTime < 1000
    })()
  )

  console.log(
    "5. 查找出现位置性能测试:",
    (() => {
      const occurrenceArray = Array.from({ length: 1000 }, (_, i) => Math.floor(i / 10))
      const occurrenceTarget = 5
      const occurrenceAlgorithms = ["findFirstOccurrence", "findLastOccurrence", "findAllOccurrences"]
      return occurrenceAlgorithms.every(algo => {
        const start = performance.now()
        searcher[algo]([...occurrenceArray], occurrenceTarget)
        const end = performance.now()
        return (end - start) < 1000 // 出现位置搜索应该在1秒内完成
      })
    })()
  )

  console.log(
    "6. 性能一致性测试:",
    (() => {
      const testArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      const target = 9
      const algorithms = ["linearSearch", "binarySearch", "interpolationSearch", "exponentialSearch", "fibonacciSearch", "jumpSearch", "ternarySearch"]
      const results = algorithms.map(algo => searcher[algo]([...testArray], target))
      return results.every(result => result === testArray.indexOf(target))
    })()
  )

  console.log(
    "7. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const start = performance.now()
          const result = searcher.binarySearch(arr, target)
          const end = performance.now()
          return result * this.multiplier
        }
      }
      const result = perfObj.processPerformance([1, 3, 5, 7, 9], 5)
      return result === 4 // 2 * 2 = 4
    })()
  )

  console.log(
    "8. 复杂对象this绑定性能测试:",
    (() => {
      const perfConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const start = performance.now()
          const result = searcher.interpolationSearch(arr, target)
          const end = performance.now()
          return result + this.config.offset
        }
      }
      const result = perfConfigObj.processWithConfig([1, 3, 5, 7, 9], 3)
      return result === 6 // 1 + 5 = 6
    })()
  )
}

testSearchPerformance()
